package com.baoli.blzj.adapter.home;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoli.blzj.R;
import com.baoli.blzj.bean.home.HomeMenuBean;

import java.util.List;

/**
 * 主页功能编辑排序适配器 by John
 */
public class HomeMainArrangeAdapter extends RecyclerView.Adapter<HomeMainArrangeAdapter.MainContentViewHolder> {

    /**
     * Item是否被选中监听
     */
    private OnCheckedChangeListener mOnCheckedChangeListener;
    /**
     * Item点击监听
     */
    private OnItemClickListener mItemOnClickListener;
    /**
     * 数据
     */
    private List<HomeMenuBean> userInfos = null;

    /**
     * Item拖拽滑动帮助
     */
    private ItemTouchHelper itemTouchHelper;

    public HomeMainArrangeAdapter() {
    }

    public HomeMainArrangeAdapter(List<HomeMenuBean> userInfos) {
        this.userInfos = userInfos;
    }

    public void notifyDataSetChanged(List<HomeMenuBean> userInfos) {
        this.userInfos = userInfos;
        super.notifyDataSetChanged();
    }

    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
        this.itemTouchHelper = itemTouchHelper;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mItemOnClickListener = onItemClickListener;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    @Override
    public MainContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainContentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_arrange, parent, false));
    }

    @Override
    public void onBindViewHolder(MainContentViewHolder holder, int position) {
        holder.setData();
    }

    @Override
    public int getItemCount() {
        return userInfos == null ? 0 : userInfos.size();
    }

    public HomeMenuBean getData(int position) {
        return userInfos.get(position);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnCheckedChangeListener {
        void onItemCheckedChange(CompoundButton view, int position, boolean checked);
    }

    class MainContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        /**
         * 名字和性别
         */
        private TextView mTvName, mTvSex;
        /**
         * 触摸就可以拖拽
         */
        private ImageView mIvTouch;
        /**
         * 是否选中
         */
        private CheckBox mCbCheck;

        public MainContentViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            //mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            //mTvSex = (TextView) itemView.findViewById(R.id.tv_sex);
            mIvTouch = (ImageView) itemView.findViewById(R.id.iv);
            //mCbCheck = (CheckBox) itemView.findViewById(R.id.cb_item_check);
            //mCbCheck.setOnClickListener(this);
            //mIvTouch.setOnTouchListener(this);
        }

        /**
         * 给这个Item设置数据
         */
        public void setData() {
            //HomeMenuBean userInfo = getData(getAdapterPosition());
            //mTvName.setText(userInfo.getName());
            //mTvSex.setText(userInfo.getSex());
            //mCbCheck.setChecked(userInfo.isCheck());
        }

        @Override
        public void onClick(View view) {
            if (view == itemView && itemTouchHelper != null) {
                mItemOnClickListener.onItemClick(view, getAdapterPosition());
            } else if (view == mCbCheck && mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onItemCheckedChange(mCbCheck, getAdapterPosition(), mCbCheck.isChecked());
            }
        }

        /*@Override
        public boolean onTouch(View view, MotionEvent event) {
            if (view == mIvTouch)
                itemTouchHelper.startDrag(this);
            return false;
        }*/
    }

}
